package com.egg.basic.enums.captcha;

import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.util.ObjectUtil;
import com.egg.common.swagger.annotation.SwaggerDisplayAnnotation;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

/**
 * 验证码样式
 */
@Getter
@SwaggerDisplayAnnotation
@AllArgsConstructor
public enum CaptchaStyleEnum {


    LINE("线段", "line", LineCaptcha.class),
    CIRCLE("圆圈", "circle", CircleCaptcha.class),
    SHEAR("扭曲", "shear", ShearCaptcha.class),

    ;

    private String message;
    private String code;
    private final Class<? extends AbstractCaptcha> clazz;

    @Override
    public String toString() {
        return message + ":" + code;
    }

    public static Optional<CaptchaStyleEnum> findByCode(String code) {
        if (ObjectUtil.isEmpty(code)) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(o -> Objects.equals(code, o.getCode())).findAny();
    }
}
